// static 生命周期有两种情况遇到
// 1. 字符串字面量
// let s: &'static str = "hello";
// 2. 作为trait bound
// fn generic<T>(x:T) where T: 'static {}
//

// 静态生命周期可以强制转转换为较短的生命周期
// 有两种方法让一个变量具有 'static 生命周期，两种变量都存储于只读内存区域
// 1. 以static 声明常量
// 2. 将字符串字面量 指定类型 &'static str

// make a constant with 'static lifetime
static NUM: i32 = 18;

// static 被强制转换为 与输入的参数相同的生命周期
fn coerce_static<'a>(_: &'a i32) -> &'a i32 {
    &NUM
}

fn main() {
    {
        let static_string = "I'm in read-only memory";
        // when `static_string` goes out of scope, the reference can no longer be used
        // but the data remains in the read-only memory
        println!("static_string: {}", static_string);
    }

    {
        let lifetime_num = 9;

        // Coerced to `'static`
        let coerced_static = coerce_static(&lifetime_num);

        println!("coerced_static: {}", coerced_static);
    }

    println!("NUM: {}", NUM);
}